package subterfuge;

import emergence.game.level.Level;
import emergence.resource.Resources;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class LeftSidebar extends JPanel {

    private static final long serialVersionUID = 1L;
    
    private final Font titleFont = new Font("Arial", Font.BOLD, 16);
    private final Font wordFont = new Font("Arial", Font.PLAIN, 12);
    
    private JLabel propLabel;
    private JPanel propLabelWrap;
    
    private JLabel nameLabel;
    private JPanel nameWrap;
    
    private JPanel lvlNamePanel;
    private JTextField lvlNameArea;
    private LevelNameListener lvlList;
    
    private Level level;

    public LeftSidebar() {
        buildGui();
    }

    private void buildGui() {
        GridBagConstraints c;
        
        propLabel = new JLabel("Level Properties");
        propLabel.setFont(titleFont);
        propLabel.setHorizontalTextPosition(JLabel.LEFT);
        propLabelWrap = new JPanel();
        propLabelWrap.setLayout(new BoxLayout(propLabelWrap, BoxLayout.LINE_AXIS));
        propLabelWrap.add(propLabel);
        propLabelWrap.add(Box.createHorizontalGlue());
        propLabelWrap.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        
        nameLabel = new JLabel("Level Name:");
        nameLabel.setFont(wordFont);
        nameWrap = new JPanel();
        nameWrap.setLayout(new BoxLayout(nameWrap, BoxLayout.LINE_AXIS));
        nameWrap.add(nameLabel);
        nameWrap.add(Box.createHorizontalGlue());
        nameWrap.setBorder(BorderFactory.createEmptyBorder(0, 15, 5, 10));
        
        lvlNamePanel = new JPanel();
        lvlNamePanel.setLayout(new BoxLayout(lvlNamePanel, BoxLayout.LINE_AXIS));
        lvlNameArea = new JTextField(20);
        lvlNameArea.setText("");
        lvlNameArea.setFont(wordFont);
        lvlNameArea.setEditable(false);
        lvlNamePanel.add(lvlNameArea);
        lvlNamePanel.setBorder(BorderFactory.createEmptyBorder(0, 15, 5, 10));
        lvlList = new LevelNameListener();
        lvlNameArea.getDocument().addDocumentListener(lvlList);
        
        this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.add(propLabelWrap, c);
        
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1.0;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.add(nameWrap, c);
        
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 1.0;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.add(lvlNamePanel, c);
        
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.NORTHWEST;
        this.add(Box.createVerticalGlue(), c);
    }

    public void loadLevel(Level level) {
        this.level = level;
        lvlNameArea.setEditable(true);
        lvlNameArea.setText(level.getName());
    }
    
    private class LevelNameListener implements DocumentListener {

        @Override
        public void insertUpdate(DocumentEvent e) {
            setName(lvlNameArea.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setName(lvlNameArea.getText());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            setName(lvlNameArea.getText());
        }
        
        private void setName(String name) {
            Resources.removeLevel(level.getName());
            level.setName(name);
            Resources.addLevel(level.getName(), level);
        }
        
    }
    
}
